import { isFunction } from 'lodash';
import {
  queryCurrent,
  changeProfile,
  queryTarget,
  queryFans,
  queryFollowing,
  follow,
  unfollow,
} from '../services/user';
import { setError } from '../utils/utils';

export default {
  namespace: 'user',

  state: {
    errorGetCurrent: false,
    errorGetUser: false,
    errorGetUserList: false,
    currentUser: {},
    user: {},
    userList: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryCurrent);
      if (response.isRight) {
        const data = response.body;
        yield put({
          type: 'changeCurrentUser',
          payload: data,
        });
        if (payload && isFunction(payload.onSuccess)) {
          payload.onSuccess(data.id);
        }
      } else {
        yield put({
          type: 'setError',
          payload: 'errorGetCurrent',
        });
      }
    },

    *fetchTarget({ payload }, { call, put }) {
      const response = yield call(queryTarget, payload.params);
      if (response.isRight) {
        const data = response.body;
        yield put({
          type: 'changeUser',
          payload: data,
        });
      } else {
        yield put({
          type: 'setError',
          payload: 'errorGetUser',
        });
      }
    },

    *fetchFansList({ payload }, { call, put }) {
      const response = yield call(queryFans, payload.params);
      if (response.isRight) {
        const data = response.body;
        yield put({
          type: 'changeUserList',
          payload: data,
        });
      } else {
        yield put({
          type: 'setError',
          payload: 'errorGetUserList',
        });
      }
    },

    *fetchFollowingList({ payload }, { call, put }) {
      const response = yield call(queryFollowing, payload.params);
      if (response.isRight) {
        const data = response.body;
        yield put({
          type: 'changeUserList',
          payload: data,
        });
      } else {
        yield put({
          type: 'setError',
          payload: 'errorGetUserList',
        });
      }
    },

    *changeProfile({ payload }, { call }) {
      const response = yield call(changeProfile, payload.params);
      if (response.isRight) {
        payload.onSuccess();
      }
    },

    *follow({ payload }, { call }) {
      const response = yield call(follow, payload.params);
      if (response.isRight) {
        payload.onSuccess();
      }
    },

    *unfollow({ payload }, { call }) {
      const response = yield call(unfollow, payload.params);
      if (response.isRight) {
        payload.onSuccess();
      }
    },
  },

  reducers: {
    setError,

    changeCurrentUser(state, { payload }) {
      return {
        ...state,
        currentUser: payload || {},
      };
    },

    changeUser(state, { payload }) {
      return {
        ...state,
        user: payload || {},
      };
    },

    changeUserList(state, { payload }) {
      return {
        ...state,
        userList: payload || [],
      };
    },
  },
};
